/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.graficaluna.dao.otros;


import dto.PersonaDTO;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author rsaldana
 */
public class PersonaDAO {

    private SqlSessionFactory sqlSessionFactory;

    public PersonaDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }
    
    public List<PersonaDTO> listarPersonas() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<PersonaDTO> list = session.selectList("personaSQL.listarPersonas");
            return list;
        }catch(Exception e){                       
            return null;
        } finally {
            session.close();
        }
    }
    
    
    public void insertarPersona(PersonaDTO p){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("personaSQL.insertarPersona", p);        
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void eliminarPersona(Integer codigo) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("personaSQL.eliminarPersona", codigo);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void actualizarPersona(PersonaDTO p) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("personaSQL.actualizarPersona", p);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public PersonaDTO obtenerPersonaPorId(Integer codigo) {   
        SqlSession session = sqlSessionFactory.openSession();
        try {            
            PersonaDTO contact = (PersonaDTO) session.selectOne("personaSQL.obtenerPersonaPorId", codigo);
            return contact;
        } finally {
            session.close();
        }
    }
    
    /*public Integer generarCodigo() {   
        SqlSession session = sqlSessionFactory.openSession();
        try {            
            Integer id = (Integer) session.selectOne("personaSQL.generarCodigo");
            return id;
        } finally {
            session.close();
        }
    }*/
    
     public List<PersonaDTO> buscarPersonasPorFiltro(String texto) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<PersonaDTO> list = session.selectList("personaSQL.buscarPersonasPorFiltro", "%"+texto+"%");
            return list;
        }catch(Exception e){                       
            return null;
        } finally {
            session.close();
        }
    }
    
}
